package com.ihg.hiex.admin.service.session.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;
import com.ihg.hiex.admin.dao.session.SessionFinanceManagementDao;
import com.ihg.hiex.admin.service.session.SessionFinanceManagementService;
import com.ihg.hiex.entity.session.Session;
import com.ihg.hiex.entity.session.during.SessionFinanceManagement;
import com.ihg.hiex.entity.session.task.SessionTask;
import com.ihg.hiex.entity.session.task.Status;
import com.ihg.hiex.entity.user.Delegate;

/**
 * 
 * @author dan
 *
 */
@Service("SessionFinanceManagementService")
public class SessionFinanceManagementServiceImpl implements SessionFinanceManagementService{
    
    @Dao
    private SessionFinanceManagementDao sessionFinanceManagementDao;
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;

    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.assessment.service.session.SessionFinanceManagementService#load(com.ihg.hiex.entity.session.Session, com.ihg.hiex.entity.user.Delegate)
     */
    public SessionFinanceManagement load(Session session, Delegate delegate) {
        return sessionFinanceManagementDao.load(session, delegate);
    }

    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.assessment.service.session.SessionFinanceManagementService#create(com.ihg.hiex.entity.session.during.SessionFinanceManagement, com.ihg.hiex.entity.session.Session, com.ihg.hiex.entity.user.Delegate)
     */
    public void create(SessionFinanceManagement financeManagement, Session session, Delegate delegate) {
        // save financeManagement
        genericDaoSupport.save(financeManagement.initilization(session, delegate));
        
    }

    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.assessment.service.session.SessionFinanceManagementService#edit(com.ihg.hiex.entity.session.during.SessionFinanceManagement)
     */
    public void edit(SessionFinanceManagement financeManagement) {
        // save financeManagement
        genericDaoSupport.update(financeManagement);
        
    }
    
    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.assessment.service.session.SessionFinanceManagementService#finish(com.ihg.hiex.entity.session.task.SessionTask, com.ihg.hiex.entity.session.during.SessionFinanceManagement)
     */
    public void finish(SessionTask sessionTask, SessionFinanceManagement sessionFinanceManagement) {
        // finish task first
        genericDaoSupport.update(sessionTask.status(Status.FINISH));
        
        // save or update SessionFinanceManagement
        if(sessionFinanceManagement.getId() != null) {
            SessionFinanceManagement original = genericDaoSupport.load(SessionFinanceManagement.class, sessionFinanceManagement.getId());
            genericDaoSupport.update(original.edit(sessionFinanceManagement));
        } else {
            genericDaoSupport.save(sessionFinanceManagement.initilization(sessionTask.getSession(), sessionTask.getDelegate()));
        }
    }

}
